package com.abu.crms.config.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;

/**
 * @author abu
 */
@ApiModel(description = "统一返回的数据格式")
@Data
public class Resp {

    @ApiModelProperty("是否成功")
    private Boolean success;

    @ApiModelProperty("返回的状态码，1表示成功，0表示失败")
    private Integer code;

    @ApiModelProperty("返回的消息")
    private String msg;

    @ApiModelProperty("返回数据")
    private HashMap<String, Object> data = new HashMap<>();

    private Resp() {
    }

    public static Resp ok() {
        Resp r = new Resp();
        r.setCode(1);
        r.setSuccess(true);
        r.setMsg("ok");
        return r;
    }

    public static Resp error() {
        Resp r = new Resp();
        r.setCode(0);
        r.setSuccess(false);
        r.setMsg("error");
        return r;
    }

    public Resp success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public Resp code(Integer code) {
        this.setCode(code);
        return this;
    }

    public Resp msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    public Resp data(HashMap<String, Object> data) {
        this.setData(data);
        return this;
    }

    public Resp data(String s1, Object data) {
        this.data.put(s1, data);
        return this;
    }

}
